package shapes3d;

import g4p_controls.G4P;
import g4p_controls.GWinApplet;
import g4p_controls.GWinData;
import g4p_controls.GWindow;
import processing.core.PApplet;

public class PickTest01 extends PApplet {

	/**
	 Simple program to demonstrate the shape picking feature
	 of this library.

	 Click on the slowly revolving cubes to change their colour.

	 created by Peter Lager
	 */

	GWindow winBuffer;

	Box[] box = new Box[10];
	Shape3D picked = null;
	boolean clicked = false;

	float bsize, a, d = 50, c = 120;

	public void setup() {
		size(400, 400, P3D);
		G4P.setCursor(CROSS);
		//cursor(CROSS);
		for (int i = 0; i < box.length; i++) {
			bsize = 5 + (int)random(12);
			box[i] = new Box(this, bsize, bsize, bsize);
			box[i].moveTo(random(-d, d), random(-d, d), random(-d, d));
			box[i].fill(randomColor());
			box[i].stroke(color(64, 0, 64));
			box[i].strokeWeight(0.6f);
			box[i].drawMode(S3D.SOLID | S3D.WIRE);
			box[i].tag = "Box " + i;
		}
		winBuffer = new GWindow(this, "Buffer", 500, 0, 400, 400, false, JAVA2D);
		winBuffer.addDrawHandler(this, "winBufferDraw");
	}

	synchronized public void winBufferDraw(GWinApplet appc, GWinData data) { //_CODE_:winBuffer:856342:
		appc.background(255);
		if(Shape3D.img != null)
			appc.image(Shape3D.img.get(),0,0);
		appc.fill(255,0,0);
		appc.noStroke();
		appc.ellipse(mouseX,  mouseY, 7, 7);
	} //_CODE_:winBuffer:856342:

	public void draw() {
		background(128);
		pushMatrix();
		camera(c * sin(a), 10, c * cos(a), 0, 0, 0, 0, 1, 0);
		for (int i = 0; i < box.length; i++)
			box[i].draw();
		if (clicked) {
			clicked = false;
			picked = Shape3D.pickShape(this, mouseX, mouseY);
			if (picked != null)
				picked.fill(randomColor());
			else
				println("Missed " + millis());
		}
		popMatrix();
		a += 0.002;
	}

	public void mouseClicked() {
		clicked = true;
	}

	int randomColor() {
		return color(random(100, 220), random(100, 220), random(100, 220));
	}
}
